﻿using UnityEngine;
using System.Collections;

public class anim : MonoBehaviour {
    float time = 0;
    float open_time = 3;
    bool is_open = false;
    public AudioClip audio_open;
    public AudioClip audio_close;
    void Start () {
	
	}
	
	void Update () {
        time += Time.deltaTime;
        if (time > open_time && is_open)
        {
            door(ref is_open, "close", audio_close,Color.red,4);
        }
	}

    void OnTriggerEnter(Collider hit)
    {
        if (hit.tag == "Player" && powercell.charges>=4)
        {
            door(ref is_open, "open", audio_open,Color.green,8);
            time = 0;
        }
    }

    void door(ref bool door_isopen, string Ani_name, AudioClip ac,Color lightcolor,float lightintensity)
    {
        transform.parent.GetComponent<Animation>().Play(Ani_name);
        door_isopen = !door_isopen;
        GetComponent<AudioSource>().clip = ac;
        GetComponent<AudioSource>().Play();
        transform.parent.GetComponent<light_cont>().changeColor(lightcolor);
        transform.parent.GetComponent<light_cont>().changeIntensity(lightintensity);
    }
}
